package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.MainDataAreaNumberListDTO;
import com.ctshk.rpc.system.dto.MainDataAreaNumberQueryDTO;
import com.ctshk.rpc.system.entity.MainDataAreaNumber;
import com.ctshk.rpc.system.mapper.MainDataAreaNumberMapper;
import com.ctshk.rpc.system.req.MainDataAreaNumberAddReq;
import com.ctshk.rpc.system.req.MainDataAreaNumberListReq;
import com.ctshk.rpc.system.req.MainDataAreaNumberQueryReq;
import com.ctshk.rpc.system.req.MainDataAreaNumberUpdateReq;
import com.ctshk.rpc.system.service.IMainDataAreaNumberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 区位区号表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-02-03
 */
@DubboService
public class MainDataAreaNumberServiceImpl extends ServiceImpl<MainDataAreaNumberMapper, MainDataAreaNumber> implements IMainDataAreaNumberService {

    /**
     * 新增区位区号
     */
    @Override
    public Result add(MainDataAreaNumberAddReq req) {
        MainDataAreaNumber mainDataAreaNumber = EntityUtil.copy(req, MainDataAreaNumber.class);
        LocalDateTime now = LocalDateTime.now();
        mainDataAreaNumber.setGmtCreate(now);
        mainDataAreaNumber.setGmtModified(now);
        mainDataAreaNumber.setIsDeleted(IsDeletedCode.NO.getCode());
        long id = IdWorker.getId();
        baseMapper.insert(mainDataAreaNumber);
        return Result.success(id);
    }

    /**
     * 编辑区位区号
     */
    @Override
    public Result update(MainDataAreaNumberUpdateReq req) {
        MainDataAreaNumber mainDataAreaNumber = EntityUtil.copy(req, MainDataAreaNumber.class);
        mainDataAreaNumber.setGmtModified(LocalDateTime.now());
        baseMapper.updateById(mainDataAreaNumber);
        return Result.success(req.getId());
    }

    /**
     * 查看区位区号
     */
    @Override
    public Result<MainDataAreaNumberQueryDTO> query(MainDataAreaNumberQueryReq req) {
        QueryWrapper<MainDataAreaNumber> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MainDataAreaNumber::getId,req.getId());
        queryWrapper.lambda().eq(MainDataAreaNumber::getIsDeleted,IsDeletedCode.NO.getCode());
        MainDataAreaNumber mainDataAreaNumber = baseMapper.selectOne(queryWrapper);
        MainDataAreaNumberQueryDTO mainDataAreaNumberQueryDTO = EntityUtil.copy(mainDataAreaNumber, MainDataAreaNumberQueryDTO.class);
        return Result.success(mainDataAreaNumberQueryDTO);
    }

    /**
     * 编辑区位区号状态
     */
    @Override
    public Result updateStatus(MainDataAreaNumberQueryReq req) {
        QueryWrapper<MainDataAreaNumber> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MainDataAreaNumber::getId,req.getId());
        queryWrapper.lambda().eq(MainDataAreaNumber::getIsDeleted,IsDeletedCode.NO.getCode());
        MainDataAreaNumber mainDataAreaNumber = baseMapper.selectOne(queryWrapper);
        if(mainDataAreaNumber.getStatus() == 1){
            mainDataAreaNumber.setStatus(2);
        } else {
            mainDataAreaNumber.setStatus(1);
        }
        baseMapper.insert(mainDataAreaNumber);
        return Result.success(req.getId());
    }

    /**
     * 查看区位区号列表
     */
    @Override
    public PageResponse<MainDataAreaNumberListDTO> list(MainDataAreaNumberListReq req) {
        Page<MainDataAreaNumber> page = new Page<>(req.getPageNo(), req.getPageSize());
        QueryWrapper<MainDataAreaNumber> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.isNotBlank(req.getName()),MainDataAreaNumber::getAreaName,req.getName());
        queryWrapper.lambda().like(StringUtils.isNotBlank(req.getAreaNumber()),MainDataAreaNumber::getAreaNumber,req.getAreaNumber());
        queryWrapper.lambda().eq(null != req.getStatus(),MainDataAreaNumber::getStatus,req.getStatus());
        queryWrapper.lambda().eq(MainDataAreaNumber::getIsDeleted,IsDeletedCode.NO.getCode());
        Page<MainDataAreaNumber> iPage = baseMapper.selectPage(page, queryWrapper);
        List<MainDataAreaNumber> records = iPage.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return new PageResponse<>(new ArrayList<>(), iPage.getCurrent() == 1,iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }
        List<MainDataAreaNumberListDTO> mainDataAreaNumberListDTOS = EntityUtil.copyList(records, MainDataAreaNumberListDTO.class);
        return new PageResponse<>(mainDataAreaNumberListDTOS, iPage.getCurrent() == 1,iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }
}
